package zen 
{
	import flash.display.BitmapData;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import org.flixel.FlxPoint;
	import org.flixel.*;
	import flash.display.BitmapDataChannel;
	/**
	 * ...
	 * @author morgan
	 */
	public class ZenGarden extends FlxSprite
	{
		[Embed (source = "../../data/zen/rake.png")] private var img_rake:Class;
		
		public var rake:BitmapData;
		public var rakeOverlay:BitmapData;
		public var lastPosition:FlxPoint = new FlxPoint(0, 0);
		public var mouseNotSet:Boolean = true;
		public function ZenGarden(x:int, y:int, image:Class) 
		{
			super(x, y, image);
			rake = FlxG.addBitmap(img_rake);
			rakeOverlay = new BitmapData(FlxG.width, FlxG.height, true, 0x00ededed);
			pixels.noise(0, 210, 255, 7, true);
		}
		
		override public function update():void 
		{
			super.update();
			
			var mx:Number = FlxG.mouse.x;
			var my:Number = FlxG.mouse.y;
			if (mouseNotSet)
			{
				lastPosition = new FlxPoint(mx, my);
				mouseNotSet = false;
			}
			if (mx == lastPosition.x && my == lastPosition.y)
			{
				return;
			}
			
			mx = lastPosition.x * 0.9 + mx * 0.1;
			my = lastPosition.y * 0.9 + my * 0.1;
			
			var realLastPosition:FlxPoint = new FlxPoint(lastPosition.x, lastPosition.y);
			var dx:Number = mx - realLastPosition.x;
			var dy:Number = my - realLastPosition.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			var maxT:Number = dist / 4;
			//lastPosition = new FlxPoint(lastPosition.x * 0.9 + mx * 0.1, lastPosition.y * 0.9 + my * 0.1);
			lastPosition = new FlxPoint(mx, my);
			var px:Number = dy / dist;
			var py:Number = -dx / dist;
			var angle:Number = Math.atan2(dy, dx) + Math.PI / 2;
			var heightScale:Number = (lastPosition.y / 480)
			for (var t:int = 0; t < maxT; t++)
			{
				var tx:Number = realLastPosition.x + dx / dist * t * 4;
				var ty:Number = realLastPosition.y + dy / dist * t * 4;
				for (var i:int = -3; i < 3; i++)
				{
					var s:Number = Math.sin(angle);
					var c:Number = Math.cos(angle);					
					var rx:int = tx + px * i * 7 * heightScale - c * 10;
					var ry:int = ty + py * i * 5 * heightScale - s * 10;
					var mat:Matrix = new Matrix(c * heightScale, s * heightScale, -s, c, rx, ry);
					pixels.draw(rake, mat, new ColorTransform(1.0, 1.0, 1.0, 0.75, 20, 20, 20, 0));
					var noise:BitmapData = new BitmapData(rake.width, rake.height);
					if (FlxG.mouse.pressed())
					{
						noise.noise(Math.random() * 65535, 210, 255, 7, true);
						pixels.draw(noise, mat, new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0));						
					}
					else
					{
						noise.noise(Math.random() * 65535, 0, 255, 7, true);
						pixels.draw(noise, mat, new ColorTransform(1, 1, 1, 0.1, 0, 0, 0, 0));
					}
				}
			}
			dirty = true;
			
		}
	}

}